<?php
include_once '../Controller/ConnectFB.php';
require '../Controller/Config.php';
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


if(is_null($fb_userData['id'])){
?>
    <script language=javascript>window.open('<?php echo $loginUrl ?>','','width=500,height=350')</script> 
<?php
    exit();
}else{
$userId = $fb_userData['id'];
}

?>
<!DOCTYPE html>
<html>
    <head>
        <link rel="shortcut icon" href="../icon/favicon.ico"/>
        <link rel="stylesheet" href="../css/myStyle.css" type="text/css" />
        <script type="text/javascript" src="../js/jquery.fixedMenu.js"></script>
   <link rel="stylesheet" type="text/css" href="../css/fixedMenu_style2.css" />
        
    </head>
<style>
body
{
        background-image:url('../images/bg.jpg');
        background-repeat:repeat-x;
        background-color:#f8e3b7;
        
        
}
</style>
    </head>
<body>

<table border="0" width="80%" height="650px" align="center" cellpadding="5" cellspacing="1" bgcolor="#404040">
  <tr height="30px">
    <td colspan="2" bgcolor="#ffffff">
        <div class="menu">
        <ul align="left">
            <table align="left" border="0">
                <tr>
                    <td>
                        <a href="../Interface/MenuHome.php">Home</a>
                    </td>
                </tr>
            </table>
            

            <table align="left" border="0">
                <tr>
                    <td>
                <a href="../Interface/MenuCreatePoll.php">Create Poll</a>
                    </td>
                </tr>
            </table>

            <table align="left" border="0">
                <tr>
                    <td>
                <a href="../Interface/MenuAnswer.php">Answer Poll</a>
                    </td>
                </tr>
            </table>
            
            <table align="left" border="0">
                <tr>
                    <td>
                <a href="../View/Compensation.php">Compensation</a>
                    </td>
                </tr>
            </table>
            
            <table align="left" border="0">
                <tr>
                    <td>
                <a href="../View/Statistics.php">Statistics</a>
                    </td>
                </tr>
            </table>
            
           <table align="left" border="0">
                <tr>
                    <td>
                <a href="../Interface/MenuAbout.php">About</a>
                    </td>
                </tr>
            </table>
            
            <table align="left" border="0">
                <tr>
                    <td>
                <a href="../Interface/MenuContact.php">Contact</a>
                    </td>
                </tr>
            </table>
        </ul>
            
            <ul align="right"> 
            <?php if($fb_user){?>
               <table align="right" border="0" >
                   <tr>
                       <td>
                           <img width="25px" higth="25px"  src="https://graph.facebook.com///<?php echo $fb_user?>/picture">
                       </td>
                       <td>
                       <font color="white" size="2" align="left"><?php echo $_SESSION['strName'] ?></font>
                <a href="<?php echo $logoutUrl?>" >Logout</a>
                </table>
                <?php }else{?>
                <table align="right" border="0" >
                    <tr>
                        <td>
                    <a href="javascript:popup('<?php echo $loginUrl?>','',500,350);">Login with Facebook</a>
                        </td>
                       </tr>
                   </table>
                <?php } ?>
              </ul> 
    </div>
    </td>
  </tr>
  <tr valign="top">
    <td width="50%" bgcolor="#ffffff" align="left">
        
        <?php
                    
//                    $q="select * from poll where poll.created_by = '1008'";
                    $q="SELECT DISTINCT
                        poll.poll_id,
                        poll.poll_title,
                        poll.compensation,
                        poll.created_date,
                        answer.user_id,
                        poll.created_by,
                        COUNT(DISTINCT answer.user_id)AS count,
                        (poll.compensation*COUNT(DISTINCT answer.user_id))AS total
                        FROM
                        poll
                        INNER JOIN answer ON answer.poll_id = poll.poll_id
                        WHERE poll.created_by = '".$userId."'
                        GROUP BY poll.poll_id";
                    $qr=mysql_query($q);
                    $num_rows = @mysql_num_rows($qr);
//                    echo $q;
                    if($num_rows==0){
                        ?>
        <table width="100%" border="0" align="center">
                            <tr valign="top" height="40px">
                                <td colspan="5" border="1" align="center" valign="middle" bgcolor="#e96c10">
                                    <font color="#ffffff" size="4" align="center"><b>แบบสอบถามที่ท่านเคยสร้าง</b></font>
                                </td>
                            </tr>
                            <tr>
                                <td align="center">
                                    <font size="4" align="left">ท่านไม่เคยสร้างแบบสอบถาม</font>
                                </td>
                            </tr>
        </table>
                        <?php   
                    }else{
                        $count =1;
                        ?>
                        <table width="100%" border="0" align="center">
                            <tr valign="top" height="40px">
                                <td colspan="5" border="1" align="center" valign="middle" bgcolor="#e96c10">
                                    <font color="#ffffff" size="4" align="center"><b>แบบสอบถามที่ท่านเคยสร้าง(รายจ่าย)</b></font>
                                </td>
                            </tr>
                        <tr align="center" bgcolor="#ee974a" height="30px">
                            <td>
                                <font color="#ffffff"><b>ลำดับที่</b></font>
                            </td>
                            <td>
                                <font color="#ffffff"><b>ชื่อแบบสอบถาม</b></font>
                            </td>
                            <td>
                                <font color="#ffffff"><b>วันที่สร้าง</b></font>
                            </td>
                            <td>
                                <font color="#ffffff"><b>จำนวนคนตอบ(คน)</b></font>
                            </td>
                            <td>
                                <font color="#ffffff"><b>ค่าตอบแบบสอบถาม(บาท)</b></font>
                            </td>
                        </tr>
                 <?php
                        while($rs=mysql_fetch_array($qr)){
                            $date = new DateTime($rs['created_date']);
//                            echo $rs['count'];
                            $people += $rs['count'];
                            $total += $rs['total'];
                        if($count%2==0){
                            $bgcolor = "#ffffff";
                        }else{
                            $bgcolor = "#f6d6a5";
                        } 
                ?>      
                        <tr align="center" bgcolor="<?php echo $bgcolor ?>"  height="25px">
                            <td>
                                <font><?php echo $count?></font>
                            </td>
                            <td>
                                <font><?php echo $rs['poll_title']?></font>
                            </td>
                            <td>
                                <font><?php echo $date->format("d/m/Y")?></font>
                            </td>
                            <td>
                                <font><?php echo number_format($rs['count']) ?></font>
                            </td>
                            <td>
                                <font><?php echo number_format($rs['compensation']) ?></font>
                            </td>
                        </tr>
                        
                <?php 
                        $count++;
                        }
                        ?>
                        <tr align="center" bgcolor="#ee974a"  height="30px">
                            <td>
                                <font><font color="#ffffff"><b>รวม</b></font>
                            </td>
                            <td>
                                
                            </td>
                            <td>
                                
                            </td>
                            
                            <td>
                                <font><?php echo number_format($people) ?></font>
                            </td>
                            <td>
                                <font><?php echo number_format($total) ?></font>
                            </td>
                        </tr>
                       </table>
                <?php
                    } 
                ?>
    <!--<img border="0" src="../images/Coming-Soon-logo.gif" alt="" width="500" height="500">-->     
    </td>
    <td bgcolor="#ffffff" align="top">
       
                <?php

                    $q="SELECT answer.answer_id,answer.poll_id,poll.poll_title,answer.answer_date,poll.compensation FROM answer 
                        LEFT JOIN poll ON poll.poll_id = answer.poll_id WHERE answer.user_id = '".$userId."' GROUP BY answer.poll_id";
                    $qr=mysql_query($q);
                    $num_rows = @mysql_num_rows($qr);
//                    echo $q;
                    if($num_rows==0){
                        
                        ?>
                    <table width="100%" border="0" align="center">
                            <tr  valign="top" height="40px">
                                <td colspan="4" border="1" align="center" valign="middle" bgcolor="#e96c10">
                                    <font color="#ffffff" size="4" align="center"><b>แบบสอบถามที่ท่านเคยตอบ(รายรับ)</b></font>
                                </td>
                            </tr>
                            <tr >
                                <td align="center">
                                    <font size="4" align="left">ท่านไม่เคยตอบแบบสอบถาม</font>
                                </td>
                            </tr>
                    </table>
                        <?php    
                    }else{
                        $count = 1;
                        ?>
                        <table width="100%" border="0" align="center">
                            <tr  valign="top" height="40px">
                                <td colspan="4" border="1" align="center" valign="middle" bgcolor="#e96c10">
                                     <font color="#ffffff" size="4" align="left"><b>แบบสอบถามที่ท่านเคยตอบ</b></font>
                                </td>
                            </tr>
                        <tr align="center" bgcolor="#ee974a" height="30px">
                            <td>
                                <font color="#ffffff"><b>ลำดับที่</b></font>
                            </td>
                            <td>
                                <font color="#ffffff"><b>ชื่อแบบสอบถาม</b></font>
                            </td>
                            <td>
                                <font color="#ffffff"><b>วันที่ตอบ</b></font>
                            </td>
                            <td>
                                <font color="#ffffff"><b>ค่าตอบแบบสอบถาม(บาท)</b></font>
                            </td>
                        </tr>
                        <?php
                        while($rs=mysql_fetch_array($qr)){  
                            $date = new DateTime($rs['answer_date']);
                            $compensation += $rs['compensation'];
                 if($count%2==0){
                            $bgcolor = "#ffffff";
                        }else{
                            $bgcolor = "#f6d6a5";
                        } 
                ?>      
                        <tr align="center" bgcolor="<?php echo $bgcolor ?>"  height="25px">
                            <td>
                                <font><?php echo $count?></font>
                            </td>
                            <td>
                                <font><?php echo $rs['poll_title']?></font>
                            </td>
                            <td>
                                <font><?php echo $date->format("d/m/Y")?></font>
                            </td>
                            <td>
                                <font><?php echo number_format($rs['compensation']) ?></font>
                            </td>                        
                        </tr>
                <?php 
                            $count++;
                        }
                        mysql_close();
                        ?>
                        <tr align="center" bgcolor="#ee974a"  height="30px">
                            <td>
                                <font><font color="#ffffff"><b>รวม</b></font>
                            </td>
                            <td>
                                
                            </td>
                            <td>
                                
                            </td>
                            <td>
                                <font><?php echo number_format($compensation) ?></font>
                            </td>
                            
                        </tr>
                        <?php
                    }
                
                ?>
                        
     </td>
  </tr>
</table>

</body>
</html>


